﻿import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import "../components"

Window {        // 迷你样本板
    id: root
    title: "筛选"
    color: "white"
    height: 590*dp
    width: 605*dp

    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint

    signal accept()
    signal reject()

    property int row: 8
    property int col: 12

    property int gridSize: 45*dp
    property var letters: ["A","B","C","D","E","F","G","H","I","J","K"]


    property var nameList: []
    property var typeList: []
    property var posiList: []
    property var nameList_filter: []
    property var typeList_filter: []
    property var posiList_filter: []

    property var selectMatrix: []       // 0: 未设置 1: 选择 2: 未选择

    onVisibleChanged: {

        nameList = CAnalysis.getList_name()
        typeList = CAnalysis.getList_type()
        posiList = CAnalysis.getList_posi()

        nameList_filter = CAnalysis.getFilterList_name()
        typeList_filter = CAnalysis.getFilterList_type()
        posiList_filter = CAnalysis.getFilterList_posi()

        nameList.splice(0, 0, "全部")
        combo_name.model = nameList
        typeList.splice(0, 0, "全部")
        combo_type.model = typeList


        selectMatrix = []
        for(let r = 0; r < row; r ++){
            selectMatrix.push([])
            for(let c = 0; c < col; c ++){
                var posiname = letters[r] + (c+1)
                if(posiList_filter.indexOf(posiname) !== -1){
                    selectMatrix[r].push(1)
                }else if(posiList.indexOf(posiname) !== -1){
                    selectMatrix[r].push(2)
                }else{
                    selectMatrix[r].push(0)
                }

            }
        }
        refreshPannel()
    }

    function refreshPannel(){
        for(let i = 0; i < col*row; i ++){
            var item = repeater.itemAt(i)
            let r = Math.floor(i / col)
            let c = i % col
            item.color = selectMatrix[r][c]===0 ? "#F6F6F6" : (selectMatrix[r][c]===1 ? "#A3AEFF" : "white")
            // 灰色蒙版覆盖
            if(selectMatrix[r][c]===0){
                var mk = item.children[1]
                mk.visible = true
            }else{
                var mk = item.children[1]
                mk.visible = false
            }
        }
    }


    Rectangle{          // 上面的表头
        height: 30*dp
        width: gridSize * col
        x: 50*dp
        y: 20*dp
        color: "#EBF3FF"
        Row{
            Repeater{
                model: col
                Item{
                    height: 30*dp
                    width: gridSize
                    Text {
                        text: index+1
                        anchors.centerIn: parent
                    }
                }
            }
        }
    }
    Rectangle{          // 左侧的表头
        height: gridSize * row
        width: 30*dp
        y: 50*dp
        x: 20*dp
        color: "#EBF3FF"
        Column{
            Repeater{
                model: row
                Item{
                    height: gridSize
                    width: 30*dp
                    Text{
                        text: letters[index]
                        anchors.centerIn: parent
                    }
                }
            }
        }
    }


    // 孔位板
    Rectangle{
        id: pannel
        y: 50*dp
        x: 50*dp
        border.color: "#CCCCCC"
        color: "#CCCCCC"
        height: row*gridSize
        width: col*gridSize

        property int maskX1: 0
        property int maskY1: 0
        property int maskX2: 0
        property int maskY2: 0
        Grid{
            columns: col
            rows: row
            columnSpacing: 1*dp
            rowSpacing: 1*dp
            Repeater{
                id: repeater
                model: row*col
                Rectangle{
                    color: "white"
                    width: gridSize-1*dp
                    height: gridSize-1*dp
                    Text{       // 有childAt(0)调用，不要改位置
                        text: letters[Math.floor(index/col)] + (index%col + 1)
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                    Rectangle{      // 灰色蒙版
                        anchors.fill: parent
                        color: "#F6F6F6"
                        visible: false
                    }
                    Rectangle{      // 选中蒙版
                        anchors.fill: parent
                        color: "#333333"
                        opacity: 0.2
                        visible: false
                    }

                    MouseArea{
                        anchors.fill: parent
                        onPressed:{
                            var mousePos = mapToItem(pannel, mouseX, mouseY)
                            pannel.maskX1 = mousePos.x
                            pannel.maskY1 = mousePos.y
                            mask.height = 0
                            mask.width = 0
                            mask.visible = true
                        }
                        onPositionChanged: {
                            var mousePos = mapToItem(pannel, mouseX, mouseY)
                            pannel.maskX2 = mousePos.x
                            pannel.maskY2 = mousePos.y
                            mask.x = Math.min(pannel.maskX1, pannel.maskX2)
                            mask.y = Math.min(pannel.maskY1, pannel.maskY2)
                            mask.height = Math.abs(pannel.maskY1 - pannel.maskY2)
                            mask.width = Math.abs(pannel.maskX1 - pannel.maskX2)

                        }
                        onReleased: {
                            var mousePos = mapToItem(pannel, mouseX, mouseY)
                            pannel.maskX2 = mousePos.x
                            pannel.maskY2 = mousePos.y
                            mask.visible = false

                            var row1 = Math.min(Math.floor(pannel.maskY1 / gridSize), Math.floor(pannel.maskY2 / gridSize))
                            var row2 = Math.max(Math.floor(pannel.maskY1 / gridSize), Math.floor(pannel.maskY2 / gridSize))
                            var col1 = Math.min(Math.floor(pannel.maskX1 / gridSize), Math.floor(pannel.maskX2 / gridSize))
                            var col2 = Math.max(Math.floor(pannel.maskX1 / gridSize), Math.floor(pannel.maskX2 / gridSize))

                            // 判断全置白还是全置黑
                            var allWhite = true
                            for(let r = row1; r <= row2; r ++){
                                for(let c = col1; c <= col2; c ++){
                                    if(selectMatrix[r][c] === 2){
                                        allWhite = false
                                        break
                                    }
                                }
                            }
                            for(let r = row1; r <= row2; r ++){
                                for(let c = col1; c <= col2; c ++){
                                    if(selectMatrix[r][c] === 0){
                                        continue
                                    }
                                    selectMatrix[r][c] = allWhite ? 2 : 1
                                }
                            }
                            refreshPannel()
                        }
                    }
                }
            }
        }
        Rectangle{
            id: mask
            color: Qt.rgba(0.8,0.8,1,0.1)
            border.color: Qt.rgba(0.8,0.8,1,0.6)
            border.width: 2*dp
        }
    }

    Row{
        y: 430*dp
        x: 30*dp
        spacing: 20*dp
        CustomComboBox{
            id: combo_name
            title: "样本名称"
            model: ["Sample1", "Sample2"]
        }
        CustomComboBox{
            id: combo_type
            title: "样本类型"
            model: ["标准品", "阴性", "未知"]
        }
    }

    Row{
        y: 500*dp
        x: 175*dp
        spacing: 30*dp
        PushButton{
            title: "取消"
            defaultMode2: true
            onReleased: {
                root.reject()
                root.close()
            }
        }
        PushButton{
            title: "确定"
            defaultMode1: true
            onReleased: {
                var selectList = []
                for(let c = 0; c < col; c ++){
                    for(let r = 0; r < row; r ++){
                        if(selectMatrix[r][c] === 1){
                            selectList.push(letters[r] + (c+1))
                        }
                    }
                }
                CAnalysis.updateFilter_posi(selectList)
                if(combo_name.getText() === "全部"){
                    CAnalysis.updateFilter_name(nameList)
                }else{
                    CAnalysis.updateFilter_name([combo_name.getText()])
                }
                if(combo_type.getText() === "全部"){
                    CAnalysis.updateFilter_type(typeList)
                }else{
                    CAnalysis.updateFilter_type([combo_type.getText()])
                }
                root.accept()
                root.close()
            }


        }
    }

}
